package ReefStats::Controller::Domains;

use strict;
use warnings;
use base 'Catalyst::Controller::FormBuilder';

sub index : Private {
    my ( $self, $c ) = @_;

    $c->forward('/domains/list');
}

sub list : Local {
    my ( $self, $c ) = @_;

    $c->stash->{domain_list} =
      [ $c->model('Domains')->search( undef, { order_by => "domain" } ) ];
    $c->stash->{template} = "domains/list.tt2";
}

sub edit : Local Form Args(1) {
    my ( $self, $c, $domain_id ) = @_;

    if ( ( defined $domain_id ) and ( $domain_id !~ /^\d+$/ ) ) {
        $c->flash->{error} = 'Invalid domain identifier provided';
        $c->response->redirect( $c->uri_for('list') );
        $c->detach();
    }

    my $domain = $c->model('Domains')->find_or_new( { id => $domain_id } );
    $c->stash->{domain} = $domain;

    if ( $self->formbuilder->submitted && $self->formbuilder->validate ) {
        $domain->visible( $self->formbuilder->field('visible') );
        $domain->update_or_insert;
        $c->flash->{message} = 'Updated domain ' . $domain->domain;
        $c->response->redirect( $c->uri_for('list') );
        $c->detach();
    }

    $self->formbuilder->field( name => 'visible', value => $domain->visible );
}

1;

__END__

=head1 NAME

ReefStats::Controller::Domains - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index 

=cut

=head1 AUTHOR

Joshua McAdams

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
